JBoss Community Archive (Read Only)

Arquillian Old

Testing an EJB

Here's a JUnit Arquillian test that validates the behavior of the EJB session bean GreetingManager. Arquillian looks up an instance of the EJB session bean in the test archive and injects it into the matching field type annotated with @EJB.

import javax.ejb.EJB;
import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class InjectionTestCase {
   @Deployment
   public static JavaArchive createTestArchive() {
      return ShrinkWrap.create(JavaArchive.class, "test.jar")
         .addClasses(GreetingManager.class, GreetingManagerBean.class);
   }

   @EJB
   private GreetingManager greetingManager;

   @Test
   public void shouldBeAbleToInjectEJB() throws Exception {
      String userName = "Earthlings";
      Assert.assertEquals(Hello " + userName, greetingManager.greet(userName));
   }
}

The TestNG version of this test looks identical, except that it extends the org.jboss.arquillian.testng.Arquillian class rather than being annotated with @RunWith.

import javax.ejb.EJB;

import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.testng.Assert;
import org.testng.annotations.Test;


@Test
public class InjectionTestCase extends Arquillian {
   @Deployment
   public static JavaArchive createTestArchive() {
      return ShrinkWrap.create(JavaArchive.class, "test.jar")
         .addClasses(GreetingManager.class, GreetingManagerBean.class);
   }

   @EJB
   private GreetingManager greetingManager;

   @Test
   public void shouldBeAbleToInjectEJB() throws Exception {
      String userName = "Earthlings";
      Assert.assertEquals(Hello " + userName, greetingManager.greet(userName));
   }
}
JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-10 12:18:46 UTC, last content change 2011-07-29 14:48:19 UTC.